package cn.zero.controller;

import cn.zero.User;
import cn.zero.feign.UserFeignClient;
import cn.zero.sentinel.OrderBlockHand;
import cn.zero.sentinel.OrderFallBack;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    // 注入feign接口
    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "order-getUserById",
            blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = OrderBlockHand.class,
            fallback = "getUserByIdFallback",
            fallbackClass = OrderFallBack.class)
    public User getUserById(@PathVariable("id") Long id){
//        if(true){
//            new RuntimeException();
//        }

//        int i = 1/0;
        // 调用OpenFeign接口，通信User服务
        User user = userFeignClient.getByUserId(id);
        System.out.println(user);
        return new User(1L,"测试","俺来了");
    }

    //必须加上一个Throwable，否则报错
//    public User getUserByIdFallback(@PathVariable("id") Long id,Throwable e){
//        // 调用OpenFeign接口，通信User服务
//        return new User(-1L, "降级触发！", "GGGGGGGGGGGGGGG！");
//    }

//    public User getUserByIdBlockHandler(@PathVariable("id") Long id, BlockException e){
//        e.printStackTrace();
//        return new User(-1L, "服务降级触发！", "顶不住啦！");
//    }
}
